package com.grandprixsw.evmsmobile;

import java.util.List;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

// change on main
public class DBHandlerTest extends
		ActivityInstrumentationTestCase2<MainActivity> {

	private Activity mActivity;
	private DBHandler db;
	
	public DBHandlerTest() {
		super("com.grandprixsw.evmsmobile", MainActivity.class);
	}
	
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mActivity = this.getActivity();
        db = new DBHandler(mActivity);
        
        cleanUp();
    }
    
    @Override
    protected void tearDown() throws Exception {
    	cleanUp();
    }
    
    private void cleanUp() {
    	db.deleteProjectsByName("TestProj%");
    }
    
	public void testSampleDB() {
		List<EVMSProject> allProjects = db.getAllProjects();
		for(EVMSProject proj : allProjects) {
			assertFalse("There should be no TestProjects in the db: " + proj.getName(),
						proj.getName().startsWith("TestProj"));
		}
        
        /**
         * CRUD Operations
         * */
        // Inserting Projects
        Log.d("Insert: ", "Inserting...");
        EVMSProject proj1 = new EVMSProject("TestProj1", Float.parseFloat("1111.1"), Float.parseFloat("1111.2"), 
        		Float.parseFloat("1111.3"), Float.parseFloat("1111.4"), Float.parseFloat("1111.5"), 
        		Float.parseFloat("1111.6"), Float.parseFloat("1111.7"));
        db.addProject(proj1);
    	
        db.addProject(new EVMSProject("TestProj2", Float.parseFloat("2222.1"), Float.parseFloat("2222.2"), 
        		Float.parseFloat("2222.3"), Float.parseFloat("2222.4"), Float.parseFloat("2222.5"), 
        		Float.parseFloat("2222.6"), Float.parseFloat("2222.7")));
        
        float bcwp3 = 150000;
        float acwp3 = 140000;
        db.addProject(new EVMSProject("TestProj3", Float.parseFloat("3333.1"), bcwp3, 
        		acwp3, Float.parseFloat("3333.4"), Float.parseFloat("3333.5"), 
        		Float.parseFloat("3333.6"), Float.parseFloat("3333.7")));
        
        db.addProject(new EVMSProject("TestProj4", Float.parseFloat("4444.1"), Float.parseFloat("4444.2"), 
        		Float.parseFloat("4444.3"), Float.parseFloat("4444.5"), Float.parseFloat("4444.5"), 
        		Float.parseFloat("4444.6"), Float.parseFloat("4444.7")));

        assertEquals("Project count should match the number added",
        			 4, db.getProjectCount());
        Log.d("Reading: ", "Get Project count...");       
        String log = "Count ====> " + db.getProjectCount();
    	Log.d("Name: ", log);


        EVMSProject project = db.getProject("TestProj1");
    	assertEquals("The first project added doesn't match the one with the name TestProj1",
    				 proj1.getName(), project.getName());
        Log.d("Reading: ", "Reading a single project by name...");       
        log = "ID: " + project.getID() + ", Name: " + project.getName() + ", BCWS: " + project.getBCWS()  
        		+ ", BCWP: " + project.getBCWP() + ", ACWP: " + project.getACWP() + ", MR: " + project.getMR()
        		+ ", EAC: " + project.getEAC() + ", BAC: " + project.getBAC() + ", CBB: " + project.getCBB();
    	Log.d("Name: ", log);

        // Reading all Projects
        Log.d("Reading: ", "Reading all projects...");
        List<EVMSProject> projects = db.getAllProjects();       
 
        assertEquals("The number of items returned by db.getAllProjects should match the number of projectsadded to the database",
        		     db.getProjectCount(), projects.size());
        for (EVMSProject proj : projects) {
            log = "ID: " + proj.getID() + ", Name: " + proj.getName() + ", BCWS: " + proj.getBCWS()  
            		+ ", BCWP: " + proj.getBCWP() + ", ACWP: " + proj.getACWP() + ", MR: " + proj.getMR()
            		+ ", EAC: " + proj.getEAC() + ", BAC: " + proj.getBAC() + ", CBB: " + proj.getCBB();
        	Log.d("Name: ", log);
        }
        
        // Test Equations.
        Equations eq = null;
        EVMSProject prj = db.getProject("TestProj3");
        if (null != prj) { 
        	eq = new Equations(prj);
			assertEquals("Projects BCWP returned incorrect value.", bcwp3, prj.getBCWP());
			assertEquals("Projects ACWP returned incorrect value.", acwp3, prj.getACWP());
			assertEquals("Cost Variance calculation is incorrect.",
						 bcwp3 - acwp3, eq.getCostVariance());
        }
	}
}
